package com.nit.lab.controller;

import com.nit.lab.dto.DtoMenu;
import com.nit.lab.dto.utils.DtoAjaxMsg;
import com.nit.lab.dto.utils.DtoDataTable;
import com.nit.lab.dto.utils.DtoDataTableReturn;
import com.nit.lab.dto.DtoUser;
import com.nit.lab.service.UserService;
import com.nit.lab.utils.SystemConstants;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.jboss.logging.Logger;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;

/**
 * @author 沈骁勇
 * @date 2017/10/9
 */
@Controller
@Scope("prototype")
@RequestMapping("/print_user")
public class UserController {
    private static Logger log = Logger.getLogger(UserController.class.getClass());

    @Resource
    private UserService userService;

    @RequestMapping(value = "/getAllUsers")
    @ResponseBody
    public DtoDataTableReturn getAllUsers(DtoDataTable dtoDataTable) {
        DtoDataTableReturn jsonList = null;
        try {
            Integer count = this.userService.getUser4DataTableCount(dtoDataTable.getSearch());
            List<DtoUser> listDto = this.userService.getUser4DataTable(dtoDataTable);
            jsonList = new DtoDataTableReturn(dtoDataTable.getDraw(), count, count, listDto);

        } catch (Exception e) {
            log.error(e.getMessage());
        }
        return jsonList;
    }

    @RequestMapping(value = "/updateUserInfo")
    public @ResponseBody DtoAjaxMsg updateEpaperInfo(DtoUser dtoUser) throws IOException {
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            Integer result = userService.updateUserInfo(dtoUser);
            if (result >= SystemConstants.Update_Success) {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_SUCCESS);
            } else {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
        }
        return dtoAjaxMsg;
    }

    @RequestMapping(value = "/insertUserInfo")
    @ResponseBody
    public DtoAjaxMsg insertEpaperInfo(DtoUser dtoUser) throws IOException {
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            Integer result = userService.insertUserInfo(dtoUser);
            if (result > SystemConstants.Insert_Success) {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_SUCCESS);
            } else {
                dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
            }
        } catch (Exception e) {
            log.error(e.getMessage());
            dtoAjaxMsg.setAjaxInfo(SystemConstants.INTERFACE_FAILUER);
        }
        return dtoAjaxMsg;
    }

    @RequestMapping(value = "/deleteUser")
    @ResponseBody
    public DtoAjaxMsg deleteUser(Integer userId) throws Exception{
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            String result = userService.deleteUserInfo(userId);
            if(result != null && "Success".equals(result)){
                dtoAjaxMsg.setAjaxInfo(SystemConstants.Success);
            }else{
                dtoAjaxMsg.setAjaxInfo(SystemConstants.Failure);
            }
        } catch (Exception e) {
            e.printStackTrace();
            dtoAjaxMsg.setAjaxInfo(SystemConstants.Failure);
        }
        return dtoAjaxMsg;
    }

    /**
     * @Description: 根据用户角色获取用户菜单
     * @Author: 卫超越
     * @Date: 2017/12/25 上午9:29
     * @return:
     */
    @RequestMapping(value = "/getMenus")
    @ResponseBody
    public DtoAjaxMsg getMenus(Integer actorId) throws Exception{
        DtoAjaxMsg dtoAjaxMsg = new DtoAjaxMsg();
        try {
            List<DtoMenu> result = userService.getMenus(actorId);
            if(result != null){
                dtoAjaxMsg.setAjaxInfo(SystemConstants.Success);
                dtoAjaxMsg.setAjaxObject(result);
            }else{
                dtoAjaxMsg.setAjaxInfo(SystemConstants.Failure);
            }
        } catch (Exception e) {
            e.printStackTrace();
            dtoAjaxMsg.setAjaxInfo(SystemConstants.Failure);
        }
        return dtoAjaxMsg;
    }

}
